import { Body, Controller, Delete, Get, HttpException, Post, Put,Param , Request, UseGuards,Req} from '@nestjs/common';
import { ApiTags,ApiOkResponse} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { UserService } from './user.service';
import { User } from '../../../db/schemas/user.schema';
import { AddUser, EditUser , UserId } from './user.dto';
import { AuthService } from '../../auth/admin.service';
import { IRequest } from 'src/interface/common';


/**用户模块控制器 */
@ApiTags('后台-用户模块')
@Controller('admin/user')
export class UserController {
	constructor(
		private readonly authService: AuthService,
		private readonly userService: UserService,

	) {}

	/**登录-jwt功能 */
	@Post('/login')
	async login(@Body() body) {
		console.log(body,'request.user');
		var token=this.authService.login(body)
		return token;
	}

	/**查-获取用户列表 */
	@ApiOkResponse({
		type:AddUser,
		isArray:true
	})
	@Get()
	@UseGuards(AuthGuard('admin-jwt'))
	async getUser(@Req() req: IRequest): Promise<User[]> {
		console.log(req.user,'req.user');

		var rows=await this.userService.findUser()
		return rows;
	}

	/**增-添加一条用户记录 */
	@Post()
	async addUser(@Body() Dto: AddUser): Promise<Boolean> {
		await this.userService.addUser(Dto)
		return true

	}

	/**改-修改一条用户记录 */
	@Put()
	async updateUser(@Body() body: EditUser): Promise<Boolean> {
		await this.userService.updateUser(body.id,body)
		return true
	}

	/**删-删除一条用户记录 */
	@Delete('/:id')
	async deleteUser(@Param() params:UserId): Promise<Boolean> {
		await this.userService.deleteUser(params.id)
		return true
	}

	/**查-查询用户下的相册 */
	@Get('/:id/photos')
	async getUserPhotos(@Param() params:UserId): Promise<any> {

		return await this.userService.getUserPhotos(params.id)
	}

	/**业务错误返回示例 */
	@Get('/error')
	testError(){
		throw new HttpException({
			code:123456,
			message:'错误示例。'
		},200)
	}
}
